package com.sp.scmdb.action;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.sp.scmdb.bean.UserBean;
import com.sp.scmdb.bean.WorkTypeBean;
import com.sp.scmdb.common.action.BaseAction;
import com.sp.scmdb.entity.WorkType;
import com.sp.scmdb.service.WorkTypeService;

@Controller("workTypeAction")
@Scope("prototype")
public class WorkTypeAction extends BaseAction<WorkTypeBean> {

	private static final long serialVersionUID = 1L;
	private static Log log = LogFactory.getLog(WorkTypeAction.class);
	
	private WorkTypeService workTypeService;

	@Autowired
	public void setWorkTypeService(WorkTypeService workTypeService) {
		this.workTypeService = workTypeService;
	}

	public String findAll() {		
		try{
			List<WorkType> depList = workTypeService.queryAll();
			entity.setAllList(depList);
			entity.setAllList(depList);
			entity.setList(depList.subList(entity.getCurrentRow(), entity.getLastRow()));
			
			this.button("Super.Popup(5,'添加类型','/SCMDB/workType/workTypeNew.jsp','',400,120)", 
					"Super.editData('/SCMDB/workTypeAction!findWorkType.action?id=',400,120)", 
					"Super.delData('/SCMDB/workTypeAction!delWorkType.action?id=')");
		} catch(RuntimeException e){			
			log.error("findAll fail!reason:" + e.getMessage());
			return "fail";
		} catch(Exception e){
			log.error("finddAll fail!reason:" + e.getMessage());
			return "error";
		}
		return SUCCESS;
	}

	/**
	 * 添加工种
	 */
	public String addWorkType() {
		try{
			UserBean user = (UserBean) session.get("user");
			String result = workTypeService.newWorkType(entity, user);
			if (log.isInfoEnabled()) {
				log.info("add suncce");
			}
			messageSession(result,"类型");
		} catch(RuntimeException e){			
			log.error("addWorkType fail!reason:" + e.getMessage());
			return "fail";
		} catch(Exception e){
			log.error("addWorkType fail!reason:" + e.getMessage());
			return "error";
		}
		return "commitSucc";
	}

	/**
	 * 跳转至工种修改页面
	 */
	public String findWorkType() {
		try{
			String depId = request.getParameter("id");
			WorkType workType = workTypeService.findID(depId);
			if(null != workType){
				entity.setId(String.valueOf(workType.getId()));
				entity.setName(workType.getName());
			} else {
				// 如果工种不存在，返回到查询页面
				return "commitSucc";
			}
		} catch(RuntimeException e){			
			log.error("findWorkType fail!reason:" + e.getMessage());
			return "fail";
		} catch(Exception e){
			log.error("findWorkType fail!reason:" + e.getMessage());
			return "error";
		}
		return "editSucc";
	}
	
	/**
	 * 更新工种
	 */
	public String editWorkType() {
		try{
			UserBean user = (UserBean) session.get("user");
			String result = workTypeService.editWorkType(entity, user);
			if (log.isInfoEnabled()) {
				log.info("edit suncce");
			}
			messageSession(result,"类型");
		} catch(RuntimeException e){			
			log.error("editWorkType fail!reason:" + e.getMessage());
			return "fail";
		} catch(Exception e){
			log.error("editWorkType fail!reason:" + e.getMessage());
			return "error";
		}
		return "commitSucc";
	}
	
	/**
	 * 删除工种
	 */
	public String delWorkType() {
		try{
			UserBean user = (UserBean) session.get("user");
			String depId = request.getParameter("id");
			if(StringUtils.isNotEmpty(depId)){
				String result = workTypeService.delWorkType(depId.split(","), user);
				messageSession(result, "类型");
			}
		} catch(RuntimeException e){			
			log.error("delWorkType fail!reason:" + e.getMessage());
			return "fail";
		} catch(Exception e){
			log.error("delWorkType fail!reason:" + e.getMessage());
			return "error";
		}
		return "commitSucc";
	}
}
